package com.jiyun.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiyun.client.BuildingClient;
import com.jiyun.client.RoomClient;
import com.jiyun.client.UserClient;
import com.jiyun.dto.RepairRecordDto;
import com.jiyun.entity.Building;
import com.jiyun.entity.RepairRecord;
import com.jiyun.entity.Room;
import com.jiyun.entity.User;
import com.jiyun.pojo.MyResult;
import com.jiyun.service.IRepairRecordService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  控制层
 * </p>
 *
 * @author 聪哥哥
 * @since 2023-04-18
 */
@RestController
@RequestMapping("/repair-record")
public class RepairRecordController {

    @Autowired
    IRepairRecordService repairRecordService;

    @Autowired
    RoomClient roomClient;

    /**
     * 添加接口
     */
    @RequestMapping("/add")
    public MyResult addRepairRecord(@RequestBody RepairRecord repairRecord){
        repairRecord.setApplyTime(LocalDateTime.now());
        repairRecordService.save(repairRecord);
        return MyResult.OK();
    }

    @RequestMapping("changeState")
    public MyResult changeState(@RequestBody RepairRecord repairRecord){
        repairRecord.setOperateTime(LocalDateTime.now());

        repairRecordService.updateById(repairRecord);
        return MyResult.OK();
    }

    /**
     * 修改接口
     */
    @RequestMapping("/update")
    public void updateRepairRecord(@RequestBody RepairRecord repairRecord){
        repairRecordService.updateById(repairRecord);
    }

    /**
     * 单个删除接口
     */
    @RequestMapping("/deleteOne")
    public void deleteOne(@RequestParam Integer id){
        repairRecordService.removeById(id);
    }

    /**
     * 批量删除接口
     * 前台ajax参数ids传入示例：1,2,3  (英文逗号分隔的字符串)
     */
    @RequestMapping("/deleteBatch")
    public void deleteBatch(@RequestParam List<Integer> ids){
        repairRecordService.removeByIds(ids);
    }

    /**
     * 根据id查询实体接口
     */
    @RequestMapping("/findById")
    public RepairRecord findById(@RequestParam Integer id){
        return repairRecordService.getById(id);
    }

    /**
     * 查询所有接口
     */
    @RequestMapping("/findAll")
    public List<RepairRecord> findAll(){
        return repairRecordService.list();
    }

    /**
     * 分页查询接口
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     */

    @Autowired
    UserClient userClient;
    @Autowired
    BuildingClient buildingClient;


    @RequestMapping("/findPage")
    public MyResult findPage(@RequestParam Integer page, @RequestParam Integer pageSize, @RequestBody RepairRecord repairRecord){
        Page<RepairRecord> repairRecordPage = new Page<>(page, pageSize);

        LambdaQueryWrapper<RepairRecord> repairRecordLambdaQueryWrapper = new LambdaQueryWrapper<>();
        repairRecordLambdaQueryWrapper.like(StringUtils.isNotBlank(repairRecord.getTitle()), RepairRecord::getTitle, repairRecord.getTitle());

        repairRecordService.page(repairRecordPage,repairRecordLambdaQueryWrapper);
        List<RepairRecord> repairRecordList = repairRecordPage.getRecords();


        Page<RepairRecordDto> repairRecordDtoPage = new Page<>();
        ArrayList<RepairRecordDto> repairRecordDtos = new ArrayList<>();
        for (RepairRecord record : repairRecordList) {
            RepairRecordDto repairRecordDto = new RepairRecordDto();
            BeanUtils.copyProperties(record,repairRecordDto);

            Room room = roomClient.findById(record.getRid());
            repairRecordDto.setRname(room.getName());

            User user = userClient.findById(record.getSid());
            repairRecordDto.setSname(user.getName());

            Building building = buildingClient.findById(record.getBid());
            repairRecordDto.setBname(building.getName());


            repairRecordDtos.add(repairRecordDto);

        }
        repairRecordDtoPage.setRecords(repairRecordDtos);
        repairRecordDtoPage.setTotal(repairRecordPage.getTotal());

        return MyResult.OK(repairRecordDtoPage);


    }

}
